package com.ruoyi.common.core.utils;

import com.ruoyi.common.core.exception.BaseException;
import lombok.NonNull;
import lombok.extern.java.Log;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author qy
 * @date 2023/4/21 14:49
 */
@Log
public class TranslationUtil {

    public static <T> void translation(@NonNull Map<String, String> map, @NonNull T t) {
        for (String s : map.keySet()) {
            try {
                Class<?> c = t.getClass().getSuperclass() == Object.class ? t.getClass() : t.getClass().getSuperclass();
                Field field = c.getDeclaredField(s);
                field.setAccessible(true);
                field.set(t, map.get(s));
            } catch (NoSuchFieldException e) {
                log.info("没有该字段");
            } catch (IllegalAccessException e) {
                throw new BaseException(e.getMessage());
            }
        }
    }

   /* public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("name","tom");
        User user = new User();
        user.setName("汤姆");
        System.out.println(user);
        translation(map, user);
        System.out.println(user);
    }
    @Data
    static class User{
        private String name;
    }*/
}
